package com.hundsun.yr.universal.p2p.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.base.BaseWebActivity;
import com.hundsun.yr.universal.library.eventbus.EventCenter;
import com.hundsun.yr.universal.library.netstatus.NetUtils;
import com.hundsun.yr.universal.library.widget.HSButton;
import com.hundsun.yr.universal.p2p.bean.MyInvestBeen;
import com.hundsun.yr.universal.p2p.bean.UserInfo;
import com.hundsun.yr.universal.p2p.config.Constants;
import com.hundsun.yr.universal.p2p.config.Extras;
import com.hundsun.yr.universal.p2p.config.TaskConstants;
import com.hundsun.yr.universal.p2p.presenter.base.RequestPresenter;
import com.hundsun.yr.universal.p2p.presenter.impl.WaitPayListPresenterImpl;
import com.hundsun.yr.universal.p2p.ui.activity.base.BaseActivity;
import com.hundsun.yr.universal.p2p.utils.BaseRequestTool2;
import com.hundsun.yr.universal.p2p.utils.CommTool;
import com.hundsun.yr.universal.p2p.utils.Logs;
import com.hundsun.yr.universal.p2p.view.base.BaseListView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by xx on 2017/6/5.
 * 待支付详情页面
 */
public class WaitPayDetailActivity extends BaseActivity implements BaseListView,View.OnClickListener{
    /**
     * 创建时间
     */
    private TextView tv_create_time;
    /**
     * 投资金额
     */
    private TextView tv_invest_amount;
    /**
     * 优惠抵扣
     */
    private TextView tv_discount;
    /**
     * 实付金额
     */
    private TextView tv_real_amount;

    /**
     * 提交
     */
    private HSButton btn_sure;

    /**
     * presenter
     */
    private RequestPresenter mPresenter;

    /**
     * 去投资数据bean
     */
    private MyInvestBeen myInvestBean;
    private String zwgt_data;

    @Override
    protected boolean isApplyKitKatTranslucency() {
        return true;
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_waitpay_detail;
    }

    @Override
    protected void initViewsAndEvents() {
        findView();
        toolbar_title.setText(getString(R.string._pay_outtime_1));
        btn_sure.setOnClickListener(this);
        toolbar_menu.setText(getString(R.string.revoke_order));

        tv_create_time.setText(CommTool.formartDate(Long.valueOf(myInvestBean.ctime) * 1000));
        tv_invest_amount.setText(getString(R.string._yuan,myInvestBean.amount));
        tv_discount.setText(getString(R.string._yuan,myInvestBean.discount_amt));
        tv_real_amount.setText(getString(R.string._yuan,myInvestBean.true_amount));
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        myInvestBean = (MyInvestBeen) extras.getSerializable("myInvestBean");

    }

    @Override
    protected void onEveComming(EventCenter eventCenter) {

    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected View getLoadingTargetView() {
        return null;
    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return true;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    public void findView(){
        tv_create_time = findView(R.id.tv_create_time);
        tv_invest_amount = findView(R.id.tv_invest_amount);
        tv_discount = findView(R.id.tv_discount);
        tv_real_amount = findView(R.id.tv_real_amount);
        btn_sure = findView(R.id.btn_sure);
        mPresenter = new WaitPayListPresenterImpl(mContext,this,"");
        toolbar_menu.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_sure://确认付款
                Logs.e("serialno" + myInvestBean.serial_no);
                zwgt_data = BaseRequestTool2.getFormatedDateString();
                JSONObject map = new JSONObject();
                try {
                    map.put("serialno", myInvestBean.serial_no);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                JSONObject jsonParams = CommTool.getJSONParams(map, sharedPreferences.getShareProf(UserInfo.TOKENID).substring(0, 24), zwgt_data);
                mPresenter.loadingData(TaskConstants.TASK_PAYUPORDER, mContext.getString(R.string.root_url_test) + mContext.getString(R.string.api_trade_test)
                        + TaskConstants.TASK_PAYUPORDER, Constants.EVENT_DATA, jsonParams, BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_POST, mContext.getString(R.string.api_trade_test)
                        + TaskConstants.TASK_PAYUPORDER, sharedPreferences.getShareProf(UserInfo.USER_NO), sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data),true);
                break;

            case R.id.toolbar_menu://撤销订单
                zwgt_data = BaseRequestTool2.getFormatedDateString();
                JSONObject map1 = new JSONObject();
                try {
                    map1.put("serialno", myInvestBean.serial_no);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                Logs.e("serialno:77777 " + myInvestBean.serial_no);

                JSONObject jsonParams1 = CommTool.getJSONParams(map1, sharedPreferences.getShareProf(UserInfo.TOKENID).substring(0, 24), zwgt_data);
                mPresenter.loadingData(TaskConstants.TASK_BIDCANCEL, mContext.getString(R.string.root_url_test) + mContext.getString(R.string.api_trade_test)
                        + TaskConstants.TASK_BIDCANCEL, Constants.EVENT_DATA, jsonParams1, BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_POST, mContext.getString(R.string.api_trade_test)
                        + TaskConstants.TASK_BIDCANCEL, sharedPreferences.getShareProf(UserInfo.USER_NO), sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data),true);
                break;
        }
    }

    @Override
    public <T> void refreshListData(List<T> data) {

    }

    @Override
    public <T> void addMoreListData(List<T> data) {

    }

    @Override
    public <T> void onFailure(String task_name) {

    }

    @Override
    public <T> void initializeViews(String task_name, T data) {
        Logs.e("data: " + data);
        switch (task_name){
            case TaskConstants.TASK_PAYUPORDER://去支付的回调，
                Bundle bundle = new Bundle();
                bundle.putInt(BaseWebActivity.BUNDLE_KEY_LOADTYPE, BaseWebActivity.LoadType.DATA.getIntValue());
                bundle.putString(BaseWebActivity.BUNDLE_KEY_TITLE,getString(R.string.pay_order));
                bundle.putString(BaseWebActivity.BUNDLE_KEY_URL,(String) data);
                bundle.putString(Extras.NAVIGATE_TAG,"INVESTLIST");
                readyGo(WebPayActivity.class, bundle);

                break;
            case TaskConstants.TASK_BIDCANCEL://订单撤销
                EventBus.getDefault().post(new EventCenter(Extras.ORDER_CANCLE));
                finish();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().removeStickyEvent(Constants.EVENT_REFRESH_DATA);
    }
}
